package cn.cvs.servive;

import cn.cvs.dao.RentMapper;
import cn.cvs.pojo.Rent;

import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class RentServive  {

    public List<Rent> gitrendList(Integer pageIndex,Integer pageSize) {
        SqlSession sqlSession = null;
        List<Rent> rentList = new ArrayList<Rent>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            rentList = sqlSession.getMapper(RentMapper.class).gitrendList(pageIndex,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return rentList;
    }

    public int delRent(Integer id) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(RentMapper.class).delRent(id);
            //            提交事务
            sqlSession.commit();
        }catch (Exception e) {
            e.printStackTrace();
            //            回滚事务
            sqlSession.rollback();
            count=0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    public int addRent(Rent rent) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(RentMapper.class).addRent(rent);
            //            提交事务
            sqlSession.commit();
        }catch (Exception e) {
            e.printStackTrace();
            //            回滚事务
            sqlSession.rollback();
            count=0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }


    public int modify(Rent rent) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(RentMapper.class).modify(rent);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }


    public Rent gitid(int id) {
        SqlSession sqlSession = null;
        //创建实体类
        Rent rent = new Rent();
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            rent = sqlSession.getMapper(RentMapper.class).gitid(id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return rent;
    }
}
